﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />
/// <reference path="PatentMember.js" />
/// <reference path="../Scripts/util.js" />
/// <reference path="../Scripts/Attachment.js" />


$.ajaxSetup({
    type: "POST",
    dataType: "text",
    global: false
});

var member;
var attachment;
var patentID;
var userID;
function Init() {
    member = new Member(
        document.getElementById("parentMemberContainer"),
        document.getElementById("hdMemberCount"),
        document.getElementById("divNewMember_0"),
        "divNewMember_",
        "removeMeber_",
        "NoMember_",
        this.patentID,
        this.userID,
        "4");

    attachment = new Attachment(
        document.getElementById("parentFileContainer"),
        document.getElementById("hdFileCount"),
        document.getElementById("divNewFile_0"),
        "divNewFile_",
        "removeFile_",
        "NoFile_",
        this.patentID,
        this.userID,
        "4");
}

$(document).ready(function () {
    patentID = $("#MainContent_ID").val();
    userID = $("#MainContent_UserID").val();
    Init();
    BindDepartment();
    $("#btnAddMember").bind("click", AddMember);
    $("#btnSave").bind("click", Back);
    $("#btnBack").bind("click", Back);
    $("#btnUpload").bind("click", ajaxFileUpload);
    $("#DepMember").change(function () {
        BindEmployee($("#DepMember").find('option:selected').val(), "#EmpMember");
    });
    LoadMembers();
    LoadAttchments();
});

function ajaxFileUpload() {
    var filePath = $("#file_upload").val()
    var fileName = filePath.replace(/.*(\/|\\)/, "");
    var fileExt = (/[.]/.exec(fileName)) ? /[^.]+$/.exec(fileName.toLowerCase()) : '';
    var exts = new Array("doc", "xls", "zip", "rar", "jpg", "jpeg", "gif","pdf");
    if (!exts.contains(fileExt)) {
        alert('不支持文件格式:' + fileExt);
        return;
    }

    $.ajaxFileUpload
    (
        {
            url: '/Handler/Attachment.ashx?' + (new Date()).valueOf(),
            secureuri: false,
            fileElementId: 'file_upload',
            contentType: 'application/x-www-form-urlencoded;charset=gb2312',
            data: { action: "addData", TableID: "4", ID: encodeURIComponent(patentID), LastModifiedBy: encodeURIComponent(userID) },
            success: function (data, status) {
                if (typeof (data.error) != 'undefined') {
                    if (data.error != '') {
                        alert(data.error);
                    } else {
                        alert(data.msg);
                    }
                }
                else {
                    attachment.addMember(fileName);
                }
            },
            error: function (data, status, e) {
                alert(e);
            }
        }
    )

    return false;
}

function Back() {
    Util.freshPage("PatentManage.aspx?ID=" + patentID);
}

function BindDepartment() {
    $.ajax({
        url: "/Handler/Department.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData" },
        success: function (data) {
            var ret = $.parseJSON(data).Departments;
            var depMember = $("#DepMember");
            depMember.empty();
            depMember.append("<option value='-1'>-请选择-</option>");
            for (var i = 0; i < ret.length; i++) {
                depMember.append("<option value='" + decodeURIComponent(ret[i].ID) + "'>" + decodeURIComponent(ret[i].Name) + "</option>");
            }
        }
    });
}

function BindEmployee(depID, emp) {
    $.ajax({
        url: "/Handler/Employee.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadDataByDepID", depID: depID },
        success: function (data) {
            var ret = $.parseJSON(data).Employees;
            var employees = $(emp);
            employees.empty();
            employees.append("<option value='-1'>-请选择-</option>");
            for (var i = 0; i < ret.length; i++) {
                var value = decodeURIComponent(ret[i].EmpID) + "|" + decodeURIComponent(ret[i].EmpName) + "|" + decodeURIComponent(ret[i].DepName)
                employees.append("<option value='" + value + "'>" + decodeURIComponent(ret[i].EmpName) + "</option>");
            }
        }
    });
}

function LoadMembers() {
    $.ajax({
        url: "/Handler/Member.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData", TableID: "4", ID: patentID },
        beforeSend: function () {
            Util.blockUI("正在加载数据...");
        },
        success: function (data) {
            Util.unblockUI();
            var ret = $.parseJSON(data).Members;
            for (var i = 0; i < ret.length; i++) {
                var memberInfo = ret[i];
                   member.addMember(memberInfo.EmpID, memberInfo.EmpName, memberInfo.DepName, "0");
            }
        }
    });
}

function LoadAttchments() {
    $.ajax({
        url: "/Handler/Attachment.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "loadData", TableID: "4", ID: patentID },
        success: function (data) {
            var ret = $.parseJSON(data).Attachments;
            for (var i = 0; i < ret.length; i++) {
                attachment.addMember(decodeURIComponent(ret[i].FileName));
            }
        }
    });
}

function AddMember() {
    var emp = $("#EmpMember").find('option:selected').val();
    if (emp == undefined) {
        return;
    }

    var arr = emp.split('|');
    if (arr[0] != -1) {
        member.addMember(arr[0], arr[1], arr[2], "0");
        AddPatentMember(arr[0], "0");
    }
}

function AddPatentMember(empID, memberType) {
    var postData =
    {
        "TableID": "4",
        "ID": encodeURIComponent(patentID),
        "EmployeeID": encodeURIComponent(empID),
        "MemberType": encodeURIComponent(memberType),
        "LastModifiedBy": encodeURIComponent(userID)
    };
    var jsonStr = $.toJSON(postData);
    $.ajax({
        url: "/Handler/Member.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "addData", jsonDataString: jsonStr },
        success: function (data) {
        }
    });
}